package com.example.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.Timestamp;

import message.Message;



public class MapEngineClient {
	Socket requestSocket;
	ObjectOutputStream out;
	ObjectInputStream in;
	Message msgIn;
	Message msgOut;
	public int connection = 0;
	public int processError = 0;
	public MapEngineClient()
	{
		try{
			//1. creating a socket to connect to the server
			String IP="172.26.186.248";
			requestSocket = new Socket(IP, 8821);
			System.out.println("Connected to "+IP+" in port 12345");
			//2. get Input and Output streams
			out = new ObjectOutputStream(requestSocket.getOutputStream());
			out.flush();
			in = new ObjectInputStream(requestSocket.getInputStream());
			try {
				this.msgIn=(Message)in.readObject();
				System.out.println(this.msgIn.getContent());
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			connection =1;
		}catch(UnknownHostException unknownHost){
			System.err.println("You are trying to connect to an unknown host!");
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		}
	}
	
	public long getAsum() {
		
		this.msgOut=new Message();
		this.msgOut.setServiceType(0);
		this.sendMessage(this.msgOut);
		long sum=0;
		try {
			this.msgIn=(Message)this.in.readObject();
			sum=this.msgIn.getServiceType();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sum;
	}
	
	/*
	 * ServiceType
	 * 1: Get own location Format: name|email|latitude|longitude|fingerprintX|fingerprintY|fingerprintZ
	 * 3: Get location of friendlist: name|email|name|email...
	 */
	public String processService(int ServiceType, String Content){
		String add=null;
		this.msgOut=new Message();
		this.msgOut.setServiceType(ServiceType);
		this.msgOut.setContent(Content);
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		this.msgOut.setTimestamp(ts.toString());
		this.sendMessage(this.msgOut);
		try{
			this.msgIn=(Message)this.in.readObject();
			if(this.msgIn.getServiceType()==ServiceType)
				add=this.msgIn.getContent();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			processError = 1;
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return add;
	}
public void test(int i) {
		
		this.msgOut=new Message();
		this.msgOut.setServiceType(2);
		this.msgOut.setContent(""+i);
		this.sendMessage(this.msgOut);
		try {
			this.msgIn=(Message)this.in.readObject();
			if(this.msgIn.getServiceType()==0)
				System.out.println("Message:"+i+" Send Sucessful!");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void close(){
		try{
			this.msgOut=new Message();
			this.msgOut.setServiceType(-1);
			this.sendMessage(this.msgOut);
			in.close();
			out.close();
			requestSocket.close();
			connection = 0;
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		}
	}
	void sendMessage(Message msg)
	{
		try{
			out.writeObject(msg);
			out.flush();
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		}
	}

}